package GUI.Ventas;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.TableColumn;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import sale.entities.OrderVO;
import sale.entities.SaleVO;
import sale.exceptions.NoSalesException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.sale.interfaces.SaleProxyMgt;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class VentasAbiertasMesas extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	SaleVO[] sales;
	int opcion;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public VentasAbiertasMesas(Shell parent, int style, int op) {
		super(parent, style);
		
		
		setText("Ventas Abiertas Mesas");
		opcion=op;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws NoSalesException 
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException, NoSalesException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws NoSalesException 
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException, NoSalesException {
		SaleProxyMgt aux = ServiceFacade.getInstance().getSaleProxyMgt();
		 sales = aux.getOpenTableSales();
				 
		System.out.println(opcion);
		shell = new Shell(getParent(), getStyle());
		shell.setSize(721, 381);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.bottom = new FormAttachment(100, -72);
		fd_table.left = new FormAttachment(0, 28);
		fd_table.top = new FormAttachment(0, 38);
		fd_table.right = new FormAttachment(0, 686);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(96);
		tblclmnNewColumn.setText("Codigo de Venta");
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(52);
		tableColumn.setText("# Mesa ");
		
		TableColumn tblclmnArticulo = new TableColumn(table, SWT.NONE);
		tblclmnArticulo.setWidth(199);
		tblclmnArticulo.setText("Articulo");
		
		TableColumn tblclmnEspecificaciones = new TableColumn(table, SWT.NONE);
		tblclmnEspecificaciones.setWidth(310);
		tblclmnEspecificaciones.setText("Especificaciones");
		
		final Button btnAgregarOrdenA = new Button(shell, SWT.NONE);
		btnAgregarOrdenA.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (opcion==1){
					System.out.println("opcion correcta");
					int mesa = Integer.parseInt(table.getSelection()[0].getText(0));
					SaleVO este=null;
					for (int i = 0; i<sales.length;i++){
						if (sales[i].getSaleKey()==mesa){
							 este = sales[i];}}
						//Construtor Shell parent, int style, SaleVO sale, int m, int op, OrderVO o
							AgregarArticulosDelivery ag = new AgregarArticulosDelivery (shell, shell.getStyle(), este, mesa,5, null);
							shell.setVisible(false);
							try {
								ag.open();
							} catch (RemoteException e1) {
								ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
								ventana.open();
								shell.dispose();
								e1.printStackTrace();
							} catch (ProxyException e1) {
								ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
								ventana.open();
								shell.dispose();
								e1.printStackTrace();
							}
						}
					
				
		/*		if (opcion ==2){
					SaleVO este = null;
					for (int i = 0; i<sales.length;i++){
						if (sales[i].getTableKey()==mesa){
							este = sales[i];
					
				}}
					CerrarVenta cerrar = new CerrarVenta (shell, shell.getStyle(), este, 2);
					shell.setVisible(false);
					cerrar.open();
			}}*/
				}}
		);
		if(opcion==0){
			btnAgregarOrdenA.setVisible(false);
		}
		if(opcion==1){
			btnAgregarOrdenA.setText("Agregar Orden a la Venta");
			
		}
		if(opcion==2){
			btnAgregarOrdenA.setText("Cerrar Esta Venta");
			
		}
		
		
		FormData fd_btnAgregarOrdenA = new FormData();
		fd_btnAgregarOrdenA.top = new FormAttachment(table, 14);
		fd_btnAgregarOrdenA.right = new FormAttachment(100, -120);
		btnAgregarOrdenA.setLayoutData(fd_btnAgregarOrdenA);
		btnAgregarOrdenA.setText("Agregar Orden a Esta Venta");
		
		if(opcion==0){
			btnAgregarOrdenA.setVisible(false);
		}
		if(opcion==1){
			btnAgregarOrdenA.setVisible(true);
		}
		
		
		for( int i = 0; i< sales.length;i++){
			System.out.println("numero de ventas abiertas" + sales.length);
			ArrayList <OrderVO> ordenes= sales[i].getOrders();	
			String m = "";
			 TableItem tableItem = new TableItem(table, SWT.NONE);
			 tableItem.setText(0, String.valueOf(sales[i].getSaleKey()));
			 tableItem.setText(1,String.valueOf(sales[i].getTableKey()));
			String esp = "";
	//		System.out.println("numero de ordenes de la venta" +ordenes.size());
				boolean alguna = false;
			 for(int j=0; j<ordenes.size();j++){
				
				 for(int k=0;k<ordenes.get(j).getArts().size();k++){
				
				 
				  m = m + " " + ordenes.get(j).getArts().get(k).getName();
				  //tableItem.setText(1,ordenes.get(j).getArts().get(k).getName());
				 
				  if (ordenes.get(j).getEspecif()==null) {
					//System.out.println("nulo");  
				  }
				  
				  
				 else{
				//	 alguna = true;
					 if(ordenes.get(j).getEspecif().get(k).equalsIgnoreCase("[")){
						 
					 }
					 else{
					//	 System.out.println(ordenes.get(j).getEspecif().get(k) + "LASD");
						 esp = esp + " " + ordenes.get(j).getArts().get(k).getName() + " " + ordenes.get(j).getEspecif().get(k);	 
					 }
					
				  }
			  }}
				 tableItem.setText(3, esp);
				 tableItem.setText(2, m);
			  
			 
		 }
	}
}
